package monpkg.rmi;

import java.io.File;
import java.rmi.Naming;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.List;

import monpkg.entities.Promotion;
import monpkg.entities.Student;

/**
 * 
 * @author Yannick Bouchot
 * @author Jonathan Romero
 *
 */

@SuppressWarnings("unused")
public class ClientRMI {
	private IPromotionImpl promotionFactory;
	private IAdminImpl adminFactory;
	private IAddressImpl addressFactory;
	private ISessionImpl sessionFactory;
	private IAnnuaireImpl annuaireFactory;
	private IStudentImpl studentFactory;
	
	String passwd;
	String nom;
	Remote r;
	

	public ClientRMI() throws Exception {

		//String url = "rmi://localhost:1099/studentFactory";
    	//studentFactory = (IStudentImpl) Naming.lookup(url);
    	//Student s = studentFactory.newStudent();
		
    	
		//System.setSecurityManager(new java.rmi.RMISecurityManager());
	}
	/**
	 * cette methode permet l'initialisation de la connection a distance
	 */
	public void init() throws Exception {
	   //System.setProperty("java.rmi.server.codebase", "http://localhost/ClientJSP/src/monpkg/rmi");


	   //System.setSecurityManager(new RMISecurityManager());
	   /*
	   try {
		   
		   Registry registry = LocateRegistry.getRegistry("127.0.0.1");
		   System.out.println();
		   //annuaireFactory = (FAnnuaireImpl) registry.lookup("studentFactory");
		   //registry.lookup("rmi://127.0.0.1:1099/studentFactory");
		   studentFactory = (FStudentImpl) registry.lookup("rmi://127.0.0.1:1099/studentFactory");;
		   
	       
	   } catch (Exception e) {
		   System.out.println("Detection d'erreur\n"+e);
	   }*/
		

    	String url = "rmi://localhost/studentFactory";
        studentFactory = (IStudentImpl) Naming.lookup(url);
        Student c = studentFactory.newStudent("NOM1", "PRENOM1", "mail@serv.com", "1234");
        //System.out.println("car model : " + c.getModel());
        //System.out.println("car number : " + c.getNumber());
        System.out.println("student counter : " + studentFactory.studentCounter());
        System.out.println("etudiant 1 : " + c.getFirstName());
        
        	
    	url = "rmi://localhost/annuaireDAO";
    	annuaireFactory = (IAnnuaireImpl) Naming.lookup(url);
    //	AnnuaireDAO annuaire = annuaireFactory.newAnnuaire();
    	//Student x = new Student("NOM1", "PRENOM1", "mail@serv.com", "1234");
    	//annuaireFactory.init();
    	//annuaireFactory.addStudent(c);
    	//System.out.println(annuaireFactory);
        annuaireFactory.findAllStudents();
    	//Student p = annuaire.findStudent(1);
        //System.out.println("car model : " + c.getModel());
        //System.out.println("car number : " + c.getNumber());
        //System.out.println("etudiant 1 : " + p.getFirstName());

	    String url1 = "rmi://localhost/annuaireDAO";
        IAnnuaireImpl annuaireFactory = (IAnnuaireImpl) Naming.lookup(url1);

	   /*
    	String url = "rmi://localhost:1099/annuaireDAO";
    	annuaireFactory = (IAnnuaireImpl) Naming.lookup(url);

    	annuaireFactory.init();
    	
    	url = "rmi://localhost/studentFactory";
    	studentFactory = (IStudentImpl) Naming.lookup(url);
    	
    	url = "rmi://localhost/promotionFactory";
    	promotionFactory = (IPromotionImpl) Naming.lookup(url);
    
    	url = "rmi://localhost/adminFactory";
    	adminFactory = (IAdminImpl) Naming.lookup(url);
    
    	url = "rmi://localhost/addressFactory";
    	addressFactory = (IAddressImpl) Naming.lookup(url);
    	
    	url = "rmi://localhost/sessionFactory";
    	sessionFactory = (ISessionImpl) Naming.lookup(url);
	
    	 */

	}
	/**
	 * Cette fonction permet de creer un etudiant a distance
	 */
	public String newStudent() throws Exception{
		Student s = studentFactory.newStudent();
		
		return "ok";
	}
	/**
	 * Cette fonction permet de trouver un etudiant a distance grace a son id
	 */
	public Student getStudent(long id) throws Exception{
		return annuaireFactory.findStudent(id);
	}
	/**
	 * Cette fonction permet de trouver tous les etudiants a distance
	 */
	public List<Student> getAllStudent() throws RemoteException{
		return annuaireFactory.findAllStudents();
	}
	/**
	 * Cette fonction permet de trouver toutes les promotions a distance
	 */
	public List<Promotion> getAllPromotion() throws RemoteException{
		return annuaireFactory.findAllPromotions();
	}
	/**
	 * Cette fonction permet de recuperer une promotion a distance
	 */
	public Promotion getPromotion(long id, boolean withStudent) throws RemoteException{
		return annuaireFactory.findPromotion(id, withStudent);
	}
	/**
	 * Cette fonction permet d'ajouter une promotion a distance
	 */
	public void addPromotion(Promotion p) throws RemoteException{
		annuaireFactory.addPromotion(p);
	}
	/**
	 * Cette fonction permet de mettre a jour une promotion a distance
	 */
	public void updatePromotion(Promotion p) throws RemoteException{
		annuaireFactory.updatePromotion(p);
	}
	/**
	 * Cette fonction permet de supprimer une promotion a distance
	 */
	public void removePromotion(long id) throws RemoteException{
		annuaireFactory.removePromotion(id);
	}
	/**
	 * Cette fonction permet d'ajouter un etudiant a distance
	 */
	public void addStudent(Student s) throws RemoteException{
		annuaireFactory.addStudent(s);
	}
	/**
	 * Cette fonction permet de mettre a jour un etudiant a distance
	 */
	public void updateStudent(Student s) throws RemoteException{
		annuaireFactory.updateStudent(s);
	}
	/**
	 * Cette fonction permet de supprimer un etudiant a distance
	 */
	public void removeStudent(long id) throws RemoteException{
		annuaireFactory.removeStudent(id);
	}
	
	
}
